<html>
<head><meta charset="utf-8"><title>Confusion over `&amp;*` · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Confusion.20over.20.60.26*.60.html">Confusion over `&amp;*`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="208091324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Confusion%20over%20%60%26%2A%60/near/208091324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Confusion.20over.20.60.26*.60.html#208091324">(Aug 26 2020 at 13:37)</a>:</h4>
<p>I know that I can use <code>&amp;*</code> to coerce a reference into a different type, if the source type has a custom <code>Deref</code> implementation. However, what does <code>&amp;*</code> do if my type does not implement <code>Deref</code>? Do they just cancel each other out so that <code>let y = &amp;*x;</code> just becomes <code>let y = x;</code> (which is what it appears to be doing), or does the Rust compiler do something subtly different which I should be aware of if I wanted to compile <code>&amp;*</code> myself?</p>



<a name="208091559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Confusion%20over%20%60%26%2A%60/near/208091559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Confusion.20over.20.60.26*.60.html#208091559">(Aug 26 2020 at 13:38)</a>:</h4>
<p><code>&amp;*x</code> where <code>x</code> has type <code>&amp;T</code> just gives you <code>&amp;T</code> again, but with a potentially shorter lifetime</p>



<a name="208091575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Confusion%20over%20%60%26%2A%60/near/208091575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Confusion.20over.20.60.26*.60.html#208091575">(Aug 26 2020 at 13:38)</a>:</h4>
<p>It does not invoke any user-defined <code>Deref</code> impl</p>



<a name="208091954"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Confusion%20over%20%60%26%2A%60/near/208091954" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Confusion.20over.20.60.26*.60.html#208091954">(Aug 26 2020 at 13:41)</a>:</h4>
<p>Never? I thought <code>&amp;*</code> on a <code>&amp;String</code> gives me a <code>&amp;str</code>, because of <code>String</code>'s <code>Deref</code> implementation?</p>



<a name="208092547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Confusion%20over%20%60%26%2A%60/near/208092547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Confusion.20over.20.60.26*.60.html#208092547">(Aug 26 2020 at 13:45)</a>:</h4>
<p>No, never. <code>&amp;*</code> on a <strong><code>String</code></strong> however will invoke its <code>Deref</code> impl and give you a <code>&amp;str</code></p>



<a name="208092948"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Confusion%20over%20%60%26%2A%60/near/208092948" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Confusion.20over.20.60.26*.60.html#208092948">(Aug 26 2020 at 13:48)</a>:</h4>
<p>Ah, I see: <code>Deref</code> implementations are only invoked if we dereference <code>T</code>, not <code>&amp;T</code>. This is very helpful. Thanks!</p>



<a name="208110019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Confusion%20over%20%60%26%2A%60/near/208110019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Confusion.20over.20.60.26*.60.html#208110019">(Aug 26 2020 at 15:48)</a>:</h4>
<p>Sometimes you'll see <code>&amp;**</code> to reach that <code>Deref</code></p>



<a name="208110974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Confusion%20over%20%60%26%2A%60/near/208110974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Confusion.20over.20.60.26*.60.html#208110974">(Aug 26 2020 at 15:56)</a>:</h4>
<p>Or <code>let y: &amp;str = &amp;some_string</code> or <code>some_string.as_str()</code> or <code>&amp;some_string[..]</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>